# coding: utf-8
from typing import Union

from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QAbstractButton, QWidget
from qfluentwidgets import PushButton, ToolTipFilter, ToolTipPosition, FluentIconBase, TransparentToolButton, \
    TransparentPushButton, ToolButton


class ToolTipButton:
    @classmethod
    def createToolTipButton(
            cls,
            button: QAbstractButton,
            text: str,
            icon: Union[QIcon, str, FluentIconBase] = None,
            tooltip: str = None,
            showDelay=300,
            position=ToolTipPosition.TOP
    ):
        button.installEventFilter(ToolTipFilter(button, showDelay, position))
        if text:
            button.setText(text)
        if icon:
            button.setIcon(icon)
        if tooltip:
            button.setToolTip(tooltip)
        return button

    @classmethod
    def pushButton(
            cls,
            parent: QWidget = None,
            text: str = None,
            icon: Union[QIcon, str, FluentIconBase] = None,
            tooltip: str = None,
            **kwargs
    ) -> PushButton:
        return cls.createToolTipButton(PushButton(parent), text, icon, tooltip, **kwargs)

    @classmethod
    def toolButton(
            cls,
            parent: QWidget = None,
            icon: Union[QIcon, str, FluentIconBase] = None,
            tooltip: str = None,
            **kwargs
    ) -> ToolButton:
        return cls.createToolTipButton(ToolButton(parent), None, icon, tooltip, **kwargs)

    @classmethod
    def transparentPushButton(
            cls,
            parent: QWidget = None,
            text: str = None,
            icon: Union[QIcon, str, FluentIconBase] = None,
            tooltip: str = None,
            **kwargs
    ) -> TransparentPushButton:
        return cls.createToolTipButton(TransparentPushButton(parent), text, icon, tooltip, **kwargs)

    @classmethod
    def transparentToolButton(
            cls,
            parent: QWidget = None,
            icon: Union[QIcon, str, FluentIconBase] = None,
            tooltip: str = None,
            **kwargs
    ) -> TransparentToolButton:
        return cls.createToolTipButton(TransparentToolButton(parent), None, icon, tooltip, **kwargs)
